//
//  LYPostDetailModel.m
//  artist
//
//  Created by laotang on 16/4/9.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYPostDetailModel.h"
#import "NSString+getStringSize.h"
#import <SDWebImageManager.h>
#import "NSDate+XMGExtension.h"
#import "NSDate+Utils.h"

@interface LYPostDetailModel ()

@property (nonatomic, assign) BOOL singlePic;

@property (nonatomic, assign) BOOL noPic;

@end

@implementation LYPostDetailModel


- (NSString *)create_time
{
    // 日期格式化类
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 设置日期格式(y:年,M:月,d:日,H:时,m:分,s:秒)
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    // 帖子的创建时间
    NSDate *create = [NSDate dateFromTimeStampString:_create_time];
    
    NSString *create_time = [create string];
    
    if (create.isThisYear) { // 今年
        if (create.isToday) { // 今天
            NSDateComponents *cmps = [[NSDate date] deltaFrom:create];
            
            if (cmps.hour >= 1) { // 时间差距 >= 1小时
                return [NSString stringWithFormat:@"%zd小时前", cmps.hour];
            } else if (cmps.minute >= 1) { // 1小时 > 时间差距 >= 1分钟
                return [NSString stringWithFormat:@"%zd分钟前", cmps.minute];
            } else { // 1分钟 > 时间差距
                return @"刚刚";
            }
        } else if (create.isYesterday) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm:ss";
            return [fmt stringFromDate:create];
        } else { // 其他
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            return [fmt stringFromDate:create];
        }
    } else { // 非今年
        return create_time;
    }
}



- (void)setImage:(NSString *)image{
    
    _image = [image copy];
    
    if (_image.length > 0) {
        self.images = [_image componentsSeparatedByString:@","];
    }else{
        
        self.noPic = YES;
    }
    
    
    
}



- (void)setImages:(NSArray *)images{
    
    _images = images;
    
    //计算图片frame
    //是否单张图片
    if (_images.count == 1) {//是
        
        self.singlePic = YES;
        
    }else{//否
        
        self.singlePic = NO;
        
    }
}


- (CGFloat)rowHeight{
    
    CGFloat rowHeight = 0;
    
    //固定高度
    rowHeight += LYFriendCellTopBarHeight;
    //    rowHeight += LYFriendCellBottomBarHeight;
    
    //content
    CGFloat contentW = LYScreenWidth - 2 * LYFriendContentMargin;
    CGFloat contentH = 0;
    
    
    CGSize maxSize = CGSizeMake(contentW, CGFLOAT_MAX);
    CGSize strSize = [_content getStringSizewithStringsFont:[UIFont systemFontOfSize:14] andMaxszie:maxSize];
    
    if (strSize.height > LYFriendContentMaxHeight && !_showContent) {
        
        contentH = LYFriendContentMaxHeight;
        _contentOverHeight = YES;
        
    }else{
        contentH = strSize.height;
        
    }
    
    //文字frame
    _contentF = CGRectMake(LYFriendContentMargin,rowHeight + LYFriendContentMargin, contentW, contentH );
    rowHeight += LYFriendContentMargin;
    rowHeight += contentH;
    
    if (_contentOverHeight) {
        _showContentBtnF = CGRectMake(LYFriendContentMargin, rowHeight, 32, LYFriendShowContentBtnHeight);
        rowHeight += LYFriendContentMargin;
        rowHeight += LYFriendShowContentBtnHeight;
    }
    
    
    //image
    CGFloat vscale = LYFriendImageMaxHeight / LYScreenHeight;
    CGFloat hscale = LYFriendImageMaxWidth / LYScreenWidth;
    
    CGFloat imageViewW = 0;
    CGFloat imageViewH = 0;
    
    //是否有图
    if (!self.noPic) {
        
        //是否为单图
        if (self.singlePic) {//是
            //是否下载完成
            if (self.downloadImage) {
                //图片屏幕比
                CGFloat imageScale = LYScreenWidth / self.downloadImage.size.width;
                
                //宽>高
                if (self.downloadImage.size.width > self.downloadImage.size.height) {
                    
                    imageViewW = self.downloadImage.size.width *  hscale * imageScale;
                    imageViewH = self.downloadImage.size.height * hscale * imageScale;
                }else{//高>宽
                    
                    
                    imageViewW = self.downloadImage.size.width * vscale * imageScale;
                    imageViewH = self.downloadImage.size.height * vscale * imageScale;
                    
                }
                //未下载完成
            }else{
                
                imageViewW = LYFriendImageMaxHeight;
                imageViewH = LYFriendImageMaxHeight;
                
            }
            //多图
        }else{
            
            NSUInteger imageCount = self.images.count;
            
            if (imageCount == 4) {
                
                imageViewW = 2 *LYFriendImageGroupHW +LYFriendImageMargin;
                imageViewH = imageViewW;
                
                
            }else{
                
                //计算行数
                NSUInteger row = (imageCount + LYFriendImageColum - 1) / LYFriendImageColum;
                
                //多行
                if (row > 1) {
                    imageViewW = LYFriendImageColum * LYFriendImageGroupHW + (LYFriendImageColum - 1) * LYFriendImageMargin;
                    imageViewH = row * LYFriendImageGroupHW + (row - 1) *LYFriendImageMargin;
                }else{
                    
                    imageViewW = imageCount * LYFriendImageGroupHW + (imageCount - 1) *LYFriendImageMargin;
                    imageViewH = LYFriendImageGroupHW;
                }
            }
            
        }
        //图片frame
        _imageViewF = CGRectMake(LYFriendContentMargin, rowHeight + LYFriendContentMargin, imageViewW, imageViewH);
        rowHeight += LYFriendContentMargin;
        rowHeight += imageViewH;
    }
    
    rowHeight += LYFriendContentMargin;
    rowHeight += LYFriendCellBottomBarHeight;
    
    return rowHeight;
    
}

@end
